/**
 * @(#)PayLogController.java 2017年7月7日
 * Copyright(C) 2017年7月7日 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.action;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.common.ResponseMapUtils;
import org.jsbd.boss.dao.impl.PayLogDaoImpl;
import org.jsbd.boss.domian.BookPayLog;
import org.jsbd.boss.domian.IpRange;
import org.jsbd.boss.domian.PayLogBean;
import org.jsbd.boss.domian.channel.ChannelApkConfig;
import org.jsbd.boss.mq.LogProcessor;
import org.jsbd.boss.service.IBookPayLogService;
import org.jsbd.boss.service.IProvinceService;
import org.jsbd.boss.util.DateUtil;
import org.jsbd.boss.util.ProvinceUtil;
import org.jsbd.boss.util.ProvinceUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import edu.hziee.common.queue.DelayExecuteBuffer;

import javax.annotation.Resource;

/**
 * 支付日志接口
 * 
 * @author <a href="mailto:zhouchao@zhexinit.com" >周超</a>
 * @version 1.0.0
 */
@Controller
@RequestMapping("/pay/log")
public class PayLogController {

	public static final Logger LOGGER = LoggerFactory.getLogger(PayLogController.class);

	@Autowired
	private DelayExecuteBuffer<PayLogBean> payLogBuffer;

	@Autowired
	private IBookPayLogService bookPayLogService;

	@Resource
	private IProvinceService provinceService;
	@Resource
	private PayLogDaoImpl payLogDao;

	@RequestMapping(value = "save")
	@ResponseBody
	public Map<String, Object> listdata(PayLogBean payLogBean) {
		
		if (payLogBean != null) {
			IpRange ipRange = provinceService.getProvinceInfo(payLogBean.getImsi(), null);
			int provinceId = ipRange.getProvinceId();
			String provinceName = ProvinceUtils.getProvinceName(provinceId+"");
			payLogBean.setProvinceId(provinceId);
			payLogBean.setProvinceName(provinceName);
			payLogBuffer.add(payLogBean);
			addPayStat(payLogBean);
			BookPayLog bookPayLog = new BookPayLog();
			bookPayLog.setImsi(payLogBean.getImsi());
			bookPayLog.setMark(payLogBean.getMark());
			bookPayLog.setMoney(new BigDecimal(payLogBean.getMoney()));
			bookPayLog.setPayContent(payLogBean.getPayContent());
			bookPayLogService.save(bookPayLog);
		}

		return ResponseMapUtils.responseSuccess();
	}
	
	
	private void addPayStat(PayLogBean payLogBean) {
		Integer userType=payLogBean.getUserType();
		if(userType==null) {
			LOGGER.error("userType为null,无法判断是新增还是留存,暂时写成留存");
			userType=2;
		}
		boolean isNew=false;
		if(userType==1) {
			isNew=true;
		}
		LogProcessor.getTM().addPayLog(DateUtil.getCurrentDateStr(), payLogBean.getMark(),payLogBean.getImsi(),isNew);
	}

	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String list() {
		return "/payInfo/list";
	}

	@RequestMapping(value = "list", params = "json")
	@ResponseBody
	public Map<String, Object> configPageList(PayLogBean payLogBean) {
		String mark = payLogBean.getMark();
		Map<String, Object> map = new HashMap<String, Object>();
		if (StringUtils.isNotBlank(mark)) {
			String[] marks = mark.split(" ");
			List<String> keywords = new ArrayList<>();
			for (String keyword : marks) {
				keywords.add(keyword);
			}
			payLogBean.setKeywords(keywords);
		}
		List<PayLogBean> list = payLogDao.selectList(payLogBean);
		Integer total = payLogDao.countOfList(payLogBean);
		map.put("rows", list);
		map.put("total", total);
		return map;
	}

}
